<?php
/*
 * mysqli类
 * 注册信息：用户名、密码、确认密码、头像上传
 * checkDtail.class.php
 * 
 * 1.静态页面
 * 2. 建表语句 if not exists user
 * 		userid int pk at
 * 		username varchar(35) 唯一
 * 		password char(100)
 * 		userface varchar(50)
 * 	3.文件有关：
 * 		头像命名规则：时间戳.jpg
 * 		保存地址  ./userface
 * 4.核心SQL：
 * 	核心SQL代码写出来
 * 		a)建表语句
 * 		b)select 用户名是否存在
 * 		c)insert ... 	
 * 5.测试提交数据
 * 6.判断哪种提交
 * 7.接收数据，并且判断合法性
 * 8.业务处理  
 * 		注释、流程图			
 * 9.结果展示
 */
//print_r($_GET);
include 'header.php';
include 'CheckDtail.class.php';
	//print_r($_POST);
	//print_r($_FILES);
	//print_r($_COOKIE);
	if(isset($_POST['username'])){
		$username = trim($_POST['username']);
		$userpwd = trim($_POST['userpwd']);
		$userpwd1 = trim($_POST['userpwd1']);
		
		// 2. 建表语句
		$con = new mysqli("localhost", "root", "flower", "ld1601","3306");
		$con->query("set names utf8");
		//创建表格
		$bool2 = $con->query("create table if not exists user(
		userid int primary key auto_increment,
		username varchar(35),
		password char(100),
		userface varchar(50)
	)default charset=utf8;");
		if($bool2){
			//echo "创建表格成功";
		}else{
			echo "创建表格失败",$con->error;
			exit();
		}
		
		//先判断用户名是否已经存在
		$obj = $con->query("select username from user");
		$obj1 = $con->query("select userid from user where username='{$username}'");
		$two_arr = array();
		while($obj_arr = $obj1->fetch_row()){
			$two_arr[] = $obj_arr;
		}
		//print_r($two_arr);
		if(!empty($two_arr)){
			echo "<script>alert('用户名已经存在');window.location='register.php';</script>";
			exit();
		}

	
		if(empty($username)||empty($userpwd)||empty($userpwd1)){
			echo "<script>alert('填写不能为空');window.location='register.php';</script>";
			exit();
		}elseif (!empty($username)&&!empty($userpwd)&&!empty($userpwd1)){
			$a = new CheckDetail();
			$name_check=$a->checkUsername($username);
			//var_dump($name_check);
			$pwd_check=$a->checkPassword($userpwd);
			//判断两次密码是否一致
			if($userpwd==$userpwd1){
				$b = true;
			}else{
				
				echo "<script>alert('两次密码不一致');window.location='register.php';</script>";
				exit();
			}
		}
		if($name_check&&$pwd_check&&$b){
				
			
			//print_r($_FILES['pic']);	
			switch ($_FILES['pic']['error']){
				case 0:
					//判断是不是图片
					$type = $_FILES['pic']['type'];
					$lastname=substr($_FILES['pic']['name'], -3,3);
					if($lastname=="jpeg"||$lastname=="jpg"||$lastname=="png"||$lastname=="gif"){
						$dest = getTimeName($_FILES['pic']['name']);
						$bool = copy($_FILES['pic']['tmp_name'], $dest);
						if($bool){
							$arr_all['pic'] = $dest;
							$_SESSION['pic'] = $dest;
							//echo $_SESSION['pic'];
						}else {
							echo "保存失败";
							exit();
						}
	
					}else{
						echo "<script>alert('上传的要是图片');</script>";
						exit();
					}
					break;
				case 1:
				case 2:
					echo "超过了指定的大小";
					break;
				case 3:
					echo "网络不稳定，请稍后再试";//只上传了部分文件
					break;
				case 4:
					echo "文件不能为空";
					break;
		}
		//存放用户信息到数据库
	
		if($obj){
			//放注册用户的信息到表里
			$userpwd = md5($userpwd);
			if(!empty($username)&&!empty($userpwd)&&!empty($arr_all['pic'])){
				$bool1 = $con->query("insert into user(username,password,userface) values('{$username}','{$userpwd}','{$arr_all['pic']}')");
				if($bool1){
					//echo "插入成功<br>";
					echo "<script>window.location='login.php';</script>";
				}else{
					unlink($dest);
					echo "插入失败<br>",$con->error;
					exit();
				}
			}
		
		}

		$con->close();
		//存放图片名到text
		
		
		
		}elseif($name_check==false&&$pwd_check==false){
			$nameerror = "用户名不合法";
			$pwderror = "密码不合法";
		}elseif($name_check==false){
			$nameerror = "用户名不合法";
				
		}elseif($pwd_check==false){
				
			$pwderror = "密码不合法";
		}
	}
	
	
	
	
?>

<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset:utf-8"/>
		<style>
			form{
			float:left;
			margin-left:500px;
			margin-top:200px}
		</style>
	</head>
	<!-- 静态页面 -->
	<body>
		<h2><center>注册</center></h2>
		<form action="" method="post" enctype="multipart/form-data">
			<table>
			<tr>
				<td>用户名：
				</td>
				<td><input type="text" name="username"/>
				<?php if (isset($nameerror)){
					echo $nameerror;
				}?>
				</td>
			</tr>
			<tr>
				<td>密码：</td>
				<td><input type="password" name="userpwd"/>
				<?php if (isset($pwderror)){
					echo $pwderror;
				}?>
				</td>
			</tr>
			<tr>
				<td>确认密码：</td>
				<td><input type="password" name="userpwd1"/>
				
				</td>
			</tr>
			<tr>
				<td>上传头像：</td>
				<td><input type="file" name="pic"/></td>
			</tr>
			
			<tr>
				<td></td>
				<td><input type="submit" value="提交"/></td>
			</tr>
			
			</table>
			<a href="login.php">登陆</a>
		</form>
	</body>
</html>